<template>
  <div class="login-container">
    <div class="logo" />
    <div class="form">
      <h1>登录</h1>
      <el-card shadow="never" class="login-card">
        <el-form :model="loginForm" :rules="loginFormRules" ref="form">
          <el-form-item label="手机号" label-width="65px" prop="mobile">
            <el-input
              placeholder="请输入账号或者手机号"
              v-model="loginForm.mobile"
            ></el-input>
          </el-form-item>
          <el-form-item label="密码" label-width="65px" prop="password">
            <el-input
              placeholder="请输入用户密码"
              show-password
              v-model="loginForm.password"
            ></el-input>
          </el-form-item>
          <el-form-item prop="ischeacd">
            <el-checkbox v-model="loginForm.ischeacd">
              用户平台使用协议
            </el-checkbox>
          </el-form-item>
          <el-form-item>
            <el-button
              type="primary"
              style="width: 350px; margin-left: 65px"
              @click="login"
              >登录</el-button
            >
          </el-form-item>
        </el-form>
       
      </el-card>
    </div>
  </div>
</template>
<script>

export default {
  name: "Login",
  data() {
    return {
      loginForm: {
        mobile: process.env.NODE_ENV === 'development' ? '13800000002' : '',
        password: process.env.NODE_ENV === 'development' ? 'hm#qd@23!' : '',
        //是否选中
        ischeacd:  process.env.NODE_ENV === 'development'
      },
      loginFormRules: {
        mobile: [
          { required: true, message: "请输入账号或者手机号", trigger: "blur" },
          //手机正则判断
          {
            pattern: /^1[3456789]\d{9}$/,
            message: "请输入正确的手机号",
            trigger: "blur",
          },
        ],
        password: [
          { required: true, message: "请输入用户密码", trigger: "blur" },
          { min: 6, max: 16, message: "密码长度在6-16位之间", trigger: "blur" },
        ],
        ischeacd: [
          // 自定义校验规则
          { validator: this.checkAccept },
        ],
      },
    };
  },
  methods: {
    //表单校验的自定义规则
    checkAccept(rule, value, callback) {
      if (value) {
        callback();
      } else {
        callback(new Error("请阅读并同意用户协议"));
      }
    },
    //用户登录方法
    async login() {
      // 只有成功校验所以表单才继续执行
      await this.$refs.form.validate();
      //调用仓库方法
      await  this.$store.dispatch("login", this.loginForm);
      //路由跳转
      this.$router.push('/')
      //提示
      this.$message.success("登录成功")
    },


  },
};
</script>
<style lang="scss">
.login-container {
  display: flex;
  align-items: stretch;
  height: 100vh;
  .logo {
    flex: 3;
    background: rgba(38, 72, 176) url(../../assets/common/login_back.png)
      no-repeat center / cover;
    border-top-right-radius: 60px;
    display: flex;
    flex-direction: column;
    align-items: flex-end;
    justify-content: center;
    padding: 0 100px;
    .icon {
      background: url(../../assets/common/logo.png) no-repeat 70px center /
        contain;
      width: 300px;
      height: 50px;
      margin-bottom: 50px;
    }
    p {
      color: #fff;
      font-size: 18px;
      margin-top: 20px;
      width: 300px;
      text-align: center;
    }
  }
  .form {
    flex: 2;
    display: flex;
    flex-direction: column;
    justify-content: center;
    padding-left: 176px;
    .el-card {
      border: none;
      padding: 0;
    }
    h1 {
      padding-left: 20px;
      font-size: 24px;
    }
    .el-input {
      width: 350px;
      height: 44px;
      .el-input__inner {
        background: #f4f5fb;
      }
    }
    .el-checkbox {
      color: #606266;
    }
  }
}
</style>
